package 设计模式.单例模式;


/**
 * @author : K k
 * @date : 9:48 2020/8/26
 */
public class Singleton_hungry {
    private  final static Singleton_hungry singleton=new Singleton_hungry();
    private int i=0;
    //或者静态代码块实现
    /*static {
        singleton=new Singleton_hungry();
    }*/

    //静态内部类实现
    private static class SingletonHolder{
        //外部类加载时，静态内部类并不会加载，只用调用getInstance时才会加载new
        private static Singleton_hungry INSTANCE=new Singleton_hungry();
        public static void action(){
        }
    }

    //内部类
    /*class InnerClass{
        InnerClass(){
            //访问外部类的final静态变量
            Singleton_hungry instance = singleton.getInstance();
            int x=i;
        }
    }*/

    private Singleton_hungry() {
    }

    public Singleton_hungry getInstance(){
        return singleton;
    }

    //静态内部类的调用
    public Singleton_hungry getInstance1(){
        return SingletonHolder.INSTANCE;
    }

}
